package xju.msg.controller;

import xju.msg.entity.User;
import xju.msg.service.UserService;
import xju.msg.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");
        User user =  userService.login(name, pwd);
        if (user != null) {
            req.getSession().setAttribute("user", user);
            resp.sendRedirect("/list");
        } else {
            req.setAttribute("error","账号或者密码有误");
            req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
        }
    }
}
